<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>我的无缝轮播</title>
    <style>
        * {
            padding: 0px;
            margin: 0px;
            list-style: none;
        }
        
        .bigbox {
            height: 200px;
            width: 400px;
            position: relative;
        }

        .clearfix::after {
            display: block;
            height: 0;
            content: "";
            clear: both;
        }
        
        .ul1 {
            width: 1600px;
            height: 200px;
        }
        
        .ul1>li {
            float: left;
            height: 200px;
            width: 400px;
            background-size: cover;
            background-repeat: no-repeat;
        }
        
        button {
            background-color: rgba(0, 0, 0, 0.5);
            height: 40px;
            width: 20px;
            position: absolute;
            top: calc(50% - 20px);
            outline: none;
            border: none;
            color: white;
            line-height: 40px;
        }
        
        .left {
            left: 0;
        }
        
        .right {
            right: 0;
        }
    </style>
</head>

<body>
    <div class="bigbox">
        <ul class="clearfix ul1">
        </ul>
        <button class="left">&lt;</button>
        <button class="right">&gt;</button>
    </div>

    <script>
        let array = [
            "http://127.0.0.1/static/images/1.jpg",
            "http://127.0.0.1/static/images/2.jpg",
            "http://127.0.0.1/static/images/3.jpg",
            "http://127.0.0.1/static/images/4.jpg"
        ]

        let ul1 = document.querySelector('.ul1');
        

        array.forEach(v => {
            let li = document.createElement("li");
            li.style.backgroundImage = `url('${v}')`;
            ul1.appendChild(li);
        })

        let lis = ul1.getElementsByTagName("li");
        ul1.style.transition = '1s'

        let intervalId = setInterval(right, 3000);

        function right() {
            if (ul1.style.transform == 'translateX(-400px)') {
                ul1.style.transition = null;
                ul1.style.transform = 'translateX(0)';
                ul1.appendChild(lis[0]);
            } else {
                ul1.style.transition = '1s'
                ul1.style.transform = 'translateX(-400px)'
            }
        }

        function left() {
            if (ul1.style.transform == 'translateX(-400px)') {
                ul1.style.transition = '1s'
                ul1.style.transform = 'translateX(0)';
            } else {
                ul1.insertBefore(lis[lis.length - 1],ul1.firstChild);
                ul1.style.transition = null;
                ul1.style.transform = 'translateX(-400px)';
                setTimeout(left,0);
            }
        }

        document.querySelector(".right").onclick = function() {
            clearInterval(intervalId);
            right();
            intervalId = setInterval(right, 3000);
        }

        document.querySelector(".left").onclick = function() {
            clearInterval(intervalId);
            left();
            intervalId = setInterval(right, 3000);
        }
    </script>
</body>

</html>